package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesCollection;
import com.kgc.collection.pojo.SalesCollectiondetail;
import com.kgc.skProvider.dao.SalesCollectionMapper;
import com.kgc.skProvider.service.SalesCollectionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SalesCollectionServiceImpl implements SalesCollectionService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SalesCollectionMapper salesCollectionMapper;

    @Override
    public ResponseMsg selectSalesCollectionByPage(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl   selectSalesCollectionByPage params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<SalesCollection> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<SalesCollection> salesCollections = salesCollectionMapper.selectSalesCollectionByPage(map);
        pageInfo.setList(salesCollections);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesCollectionServiceImpl   selectSalesCollectionByPage params is {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertSalesCollection(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl  insertSalesCollection params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        SalesCollection salesCollection =mapper.convertValue(map.get("salesCollection"), SalesCollection.class);
        List<?> rawDetails = (List<?>) map.get("details");

        List<SalesCollectiondetail> details=mapper.convertValue(rawDetails,new TypeReference<List<SalesCollectiondetail>>(){});
        int result1 = salesCollectionMapper.insertSalesCollection(salesCollection);
        try{
            for (SalesCollectiondetail detail : details) {
                salesCollectionMapper.insertSalesCollectionDetail(detail);
            }
            responseMsg.setCode("200");
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result1>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionServiceImpl  insertSalesCollection params is {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesCollectionDetail(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl  selectSalesCollectionDetail params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        List<SalesCollectiondetail> list=salesCollectionMapper.selectSalesCollectionDetailByPage(map);
        responseMsg.setData(list);
        logger.debug("SalesCollectionServiceImpl  selectSalesCollectionDetail params is {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateSalesCollection(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl  updateSalesCollection params is {}",map.toString());
        deleteSalesCollection(map);
        ResponseMsg responseMsg  = insertSalesCollection(map);
        logger.debug("SalesCollectionServiceImpl  updateSalesCollection params is {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteSalesCollection(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl  deleteSalesCollection params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1 = salesCollectionMapper.deleteSalesCollection(map);
        int result2 = salesCollectionMapper.deleteSalesCollectionDetail(map);
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionServiceImpl  deleteSalesCollection params is {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg checkSalesCollection(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl  checkSalesCollection params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result= salesCollectionMapper.checkSalesCollection(map);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionServiceImpl  checkSalesCollection params is {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesCollectionByCode(Map<String, Object> map) {
        logger.info("SalesCollectionServiceImpl  selectSalesCollectionByCode params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        List<SalesCollectiondetail> list=salesCollectionMapper.selectSalesCollectionDetailByPage(map);
        SalesCollection salesCollection =salesCollectionMapper.selectSalesCollectionByCode(map);
        salesCollection.setDetails(list);
        responseMsg.setData(salesCollection);
        logger.debug("SalesCollectionServiceImpl  selectSalesCollectionByCode params is {}",responseMsg.toString());
        return responseMsg;
    }
}
